
// Copyright (C) 2007 Richard Uhler
// This file is part of Esiply.
//
// Esiply is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <assert.h>

#include "ErrorObject.h"
#include "HostContext.h"
#include "HostValue.h"
#include "Object.h"
#include "Scope.h"
#include "SourceElements.h"
#include "String.h"
#include "Value.h"
#include "parser_wrap.h"
#include <pthread.h>

extern int yyparse(SourceElements** prog);
extern void yyrestart(FILE* file);
extern void yy_scan_string(const char* str);
extern String* curr_file;

HostContext::HostContext(Scope* scope)
    : m_scope(scope)
{}

HostContext::HostContext(const HostContext& rhs)
{
    m_scope = rhs.m_scope;
}

HostContext& HostContext::operator=(const HostContext& rhs)
{
    m_scope = rhs.m_scope;
    return *this;
}
static pthread_mutex_t parse_lock = PTHREAD_MUTEX_INITIALIZER; 
HostValue HostContext::run_file(const std::string& filename)
{
    FILE* fin = fopen(filename.c_str(), "r");
    if (!fin)
    {
        // TODO: handle this case better.
        fprintf(stderr, "Unable to open %s\n", filename.c_str());
        assert(false && "Can't find file to run");
    }

    yylloc.first_line = 1;
    yylloc.last_line = 1;
    yylloc.first_column = 0;
    yylloc.last_column = 0;
    curr_file = new String(filename.c_str());
    yyrestart(fin);

    SourceElements* prog = NULL;
	//avoid multi into one yyparse, TODO:: reentrant parse
	pthread_mutex_lock(&parse_lock);
    int parse = yyparse(&prog);
	pthread_mutex_unlock(&parse_lock); 
    fclose(fin);
    if (parse != 0)
    {
        fprintf(stderr, "Syntax error in %s around line %i\n", 
                filename.c_str(), yylloc.last_line);
        return esiply_throw(ET_SYNTAX, "Syntax error attempting to execute file.");
    }

    assert(prog);
    return prog->run(m_scope);
}

HostValue HostContext::run_string(const char* str)
{
    curr_file = new String(str);
    yy_scan_string(str);
    SourceElements* prog = NULL;
	//avoid multi into one yyparse, TODO:: reentrant parse
    pthread_mutex_lock(&parse_lock);
    int parse = yyparse(&prog);
	pthread_mutex_unlock(&parse_lock); 
    if (parse != 0)
    {
        return esiply_throw(ET_SYNTAX, str);
    }
    assert(prog);
    return prog->run(m_scope);
}
    
HostValue HostContext::host_eval(const char* str, HostValue* val1,
        HostValue* val2, HostValue* val3, HostValue* val4)
{
    // Add $1...$4 to the local scope
    Object* var = m_scope->get_variable();

    if (val1)
    {
        var->put(new String("$1"), val1->get_value(), PA_DONT_ENUM);
    }
    if (val2)
    {
        var->put(new String("$2"), val2->get_value(), PA_DONT_ENUM);
    }
    if (val3)
    {
        var->put(new String("$3"), val3->get_value(), PA_DONT_ENUM);
    }
    if (val4)
    {
        var->put(new String("$4"), val4->get_value(), PA_DONT_ENUM);
    }

    // Evaluate the string
    HostValue ret = run_string(str);

    // fill in the objects
    if (val1) val1->set_value(var->get(new String("$1")));
    if (val2) val2->set_value(var->get(new String("$2")));
    if (val3) val3->set_value(var->get(new String("$3")));
    if (val4) val4->set_value(var->get(new String("$4")));
    return HostValue(ret);
}

Scope* HostContext::get_scope() const
{
    return m_scope;
}

